//
//  textView.m
//  WLScrollView
//
//  Created by 张子豪 on 2017/11/17.
//  Copyright © 2017年 张子豪. All rights reserved.
//

#import "textView.h"

@implementation textView

- (instancetype)initWithFrame:(CGRect)frame Identifier:(NSString *)indentifier
{
    self = [super initWithFrame:frame Identifier:indentifier];
    if (self) {
        
        self.backgroundColor = CSWhiteColor;
        
        /**
         滚动
         */
        UIScrollView *scrollView= [UIScrollView new];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        /**
         编辑
         */
        UIButton *eidtBtn = [UIButton new];
        [eidtBtn setTitle:@"编辑"];
        [eidtBtn setTitleColor:BlackColor];
        eidtBtn.titleLabel.font = MidBigFont;
        [scrollView addSubview:eidtBtn];
        self.eidtBtn = eidtBtn;
        
        /**
         头像
         */
        UIButton *iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [iconBtn setImage:PlaceHold forState:UIControlStateNormal];
        iconBtn.layer.cornerRadius = 25;
        iconBtn.clipsToBounds = YES;
        [scrollView addSubview:iconBtn];
        self.iconBtn = iconBtn;
        
        /**
         名字
         */
        UILabel *stutyLable = [UILabel new];
        stutyLable.text = @"陈志强的学员";
        stutyLable.font = TitleBoldFont;
        stutyLable.textColor = BlackColor;
        [scrollView addSubview:stutyLable];
        self.stutyLable = stutyLable;
        
        /**
         //常去门店tag
         */
        //第三方标签布局的视图
        SKTagView *tagView = [[SKTagView alloc] init];
        // 整个tagView对应其SuperView的上左下右距离
        tagView.padding = UIEdgeInsetsMake(0, 0, 0, 0);
        // 上下行之间的距离
        tagView.lineSpacing = 5;
        // item之间的距离
        tagView.interitemSpacing = 5;
        // 最大宽度
        tagView.preferredMaxLayoutWidth = kScreenWidth/2 ;
        //        tagView.regularWidth = kScreenWidth - 80;
        //    @property (assign, nonatomic) CGFloat regularWidth; //!< 固定宽度
        //    @property (nonatomic,assign ) CGFloat regularHeight; //!< 固定高度
        // 原作者没有能加固定宽度的，自己修改源码加上了固定宽度和高度,默认是0，就是标签式布局，如果实现了，那么就是固定宽度高度
        //    self.tagView.regularWidth = 100;
        //            tagView.regularHeight = 30;
        [scrollView addSubview:tagView];
        
        self.stutyTypeSKtagView = tagView;
        
        /**
         健身成果
         */
        UILabel *fitLable = [UILabel new];
        fitLable.text = @"健身成果";
        fitLable.font = MidBigFont;
        fitLable.textColor = BlackColor;
        [scrollView addSubview:fitLable];
        self.fitLable = fitLable;
        
        /**
         健身成果内容
         */
        UILabel *fitTitleLable = [UILabel new];
        fitTitleLable.text = @"健身成果sa发放到福建阿斯偶的感觉哦我爱神的箭格拉斯看到价格可拉伸的价格可拉伸的价格卡拉时间管理侃大山公交卡麓山国际";
        fitTitleLable.font = MidBigFont;
        fitTitleLable.numberOfLines = 0;
        fitTitleLable.textColor = GrakColor;
        [scrollView addSubview:fitTitleLable];
        self.fitTitleLable = fitTitleLable;
        
        /**
         健身前后
         */
        UILabel *fitABLable = [UILabel new];
        fitABLable.text = @"健身前后";
        fitABLable.font = MidBigFont;
        fitABLable.numberOfLines = 0;
        fitABLable.textColor = BlackColor;
        [scrollView addSubview:fitABLable];
        self.fitABLable = fitABLable;
        
        /**
         //放图片面板
         */
        JGGView *jggView = [JGGView new];
        [scrollView addSubview:jggView];
        self.jggView = jggView;
        [self createUI];
    }
    return self;
}

- (void)createUI{
    
    
    /**
     滚动
     */
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
        
    }];

    
    /**
     编辑
     */
    [self.eidtBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.scrollView).offset(5);
        make.left.width.mas_equalTo(self.scrollView);
        
    }];
    
    
    /**
     头像
     */
    [self.iconBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.eidtBtn.mas_bottom).offset(5);
        make.left.mas_equalTo(self.scrollView).offset(15);
        make.width.height.mas_equalTo(50);
        
        
    }];
    
    /**
     名字
     */
    [self.stutyLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.iconBtn);
        make.left.mas_equalTo(self.iconBtn.mas_right).offset(15);
        
        
    }];
    
    /**
     学员类型
     */
    [self.stutyTypeSKtagView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.stutyLable.mas_bottom).offset(10);
        make.left.mas_equalTo(self.stutyLable);
        make.right.mas_equalTo(self.scrollView).offset(-20);
        
        
    }];
    
    /**
     健身成果
     */
    [self.fitLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.stutyTypeSKtagView.mas_bottom).offset(15);
        make.left.mas_equalTo(self.iconBtn);
        make.right.mas_equalTo(self.stutyTypeSKtagView);
        
        
    }];
    
    /**
     健身成果内容
     */
    [self.fitTitleLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.fitLable.mas_bottom).offset(10);
        make.left.mas_equalTo(self.iconBtn);
        make.right.mas_equalTo(self.stutyTypeSKtagView);
        
        
    }];
    
    /**
     健身前后
     */
    [self.fitABLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.fitTitleLable.mas_bottom).offset(10);
        make.left.mas_equalTo(self.iconBtn);
        make.right.mas_equalTo(self.stutyTypeSKtagView);
        
        
    }];
    
    /**
     //放图片面板
     */
    [self.jggView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.fitABLable.mas_bottom).offset(10);
        make.left.mas_equalTo(self.iconBtn);
        make.right.mas_equalTo(self.stutyTypeSKtagView);
//        make.bottom.lessThanOrEqualTo(self).offset(-10);
        
    }];

    
    


}

-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    
    NSString *tags = [dic objectForKey:@"Tag"];
    
    [self.stutyTypeSKtagView removeAllTags];
    
    if (tags.length > 0) {
        NSArray *arrM = [tags componentsSeparatedByString:@","];
        // 开始加载
        [arrM enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 初始化标签
            SKTag *tag = [[SKTag alloc] initWithText:arrM[idx]];
            // 标签相对于自己容器的上左下右的距离
            tag.padding = UIEdgeInsetsMake(3, 5, 3, 5);
            // 弧度
            tag.cornerRadius = 3.0f;
            // 字体
            tag.font = MidFont;
            // 边框宽度
            tag.borderWidth = 1;
            // 背景
            //        tag.bgColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
            // 边框颜色
            tag.borderColor = CSColorFromRGB(0xdbdbdb);
            // 字体颜色
            tag.textColor = BlackColor;
            // 是否可点击
            tag.enable = YES;
            // 加入到tagView
            [self.stutyTypeSKtagView addTag:tag];
        }];
    }
    
    self.stutyLable.text = [NSString stringWithFormat:@"%@的学员",[dic objectForKey:@"AddUserID"]];
    
    [self.iconBtn sd_setImageWithURL:[NSURL URLWithString:[Helper imageUrlStringToChinese:[dic objectForKey:@"Avatar"] imageType:nil]] forState:UIControlStateNormal placeholderImage:PlaceHold];
    
    self.fitTitleLable.text = [dic objectForKey:@"Description"];
    
    NSString *photoStr ;
    
    NSString *befor = [dic objectForKey:@"BeforePic"];
    
    NSString *after = [dic objectForKey:@"AfterPic"];
    
    if (after.length > 0 && befor.length > 0) {
        photoStr = [NSString stringWithFormat:@"%@,%@",befor,after];
    }else if (after.length == 0 && befor.length > 0)
    {
        photoStr = befor;
        
    }
    else if (after.length > 0 && befor.length == 0)
    {
        photoStr = after;
        
    }
    
    
    if (photoStr.length > 0) {
        
    //布局九宫格
        [self.jggView JGGView:self.jggView DataSource:[photoStr componentsSeparatedByString:@","] completeBlock:^(NSInteger index, NSArray *dataSource,NSIndexPath *indexpath) {

        }];
        
    }
    
    
    
    
    
    
//
//    [self setNeedsLayout];
//    [self layoutIfNeeded];
}

-(void)layoutSubviews
{
    
//    self.scrollView.contentSize = CGSizeMake(self.width, 800 );
    
//    [self needsUpdateConstraints];
    
    self.scrollView.contentSize = CGSizeMake(self.width, self.jggView.bottom + 10 );
    
}

@end
